package linuxbox.common.taglib;

import java.util.*;
import java.text.SimpleDateFormat;
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;

import linuxbox.*;

/**
 * This class displays a Hour list in the SELECT tag.
 */
public class HourList
    extends TagSupport
{
  protected boolean allowNull = false;
  protected int selected = -1;
  protected boolean showampm = true;

  public int doStartTag() throws JspException
  {
    try
    {
      JspWriter out = pageContext.getOut();

      if (allowNull)
      {
        out.print("<option value=-1>Hour</option>");

      }

      Calendar tmpHour = Calendar.getInstance();
      SimpleDateFormat dateTimeFormatter = new SimpleDateFormat(BoxStatic.VERYSHORT_HOUR_FORMAT, Locale.US);
      //Just use the default Calendar here
//      dateTimeFormatter.setCalendar(tmpHour);
      if (showampm)
      {
        dateTimeFormatter.applyPattern(BoxStatic.SHORT_HOUR_FORMAT);
      }
      tmpHour.set(Calendar.HOUR_OF_DAY, 0);
      for (int i = 0; i < 24; i++)
      {
        if (selected == i)
        {
          out.print("<option value=\"" + i + "\" selected>" +
                    dateTimeFormatter.format(tmpHour.getTime()) +
                    "</option>");
        }
        else
        {
          out.print("<option value=\"" + i + "\">" +
                    dateTimeFormatter.format(tmpHour.getTime()) +
                    "</option>");
        }
        tmpHour.add(Calendar.HOUR_OF_DAY, 1);
      }

      /*
            String[] ampm = BoxStatic.dateTimeSymbols.getAmPmStrings();

            out.print("<option value=0 ");
            if ( selected == 0)
              out.print(" selected ");
            out.print(">12");
            out.print(ampm[Calendar.AM]);
            out.print("</option>");

            for(int i=1;i<=11;i++)
            {
              out.print("<option value=");
              out.print(i);
              if ( selected == i)
                out.print(" selected ");
              out.print(">");
              out.print(i);
              out.print(ampm[Calendar.AM]);
              out.print("</option>");
            }

            out.print("<option value=12 ");
            if ( selected == 12)
              out.print(" selected ");
            out.print(">12");
            out.print(ampm[Calendar.PM]);
            out.print("</option>");

            for(int i=13;i<=24;i++)
            {
              out.print("<option value=");
              out.print(i);
              if ( selected == i)
                out.print(" selected ");
              out.print(">");
              out.print(i-12);
              out.print(ampm[Calendar.PM]);
              out.print("</option>");
            }
       */

      out.flush();
    }
    catch (Exception e)
    {
      e.printStackTrace();
      throw new JspException(e.getMessage());
    }

    return SKIP_BODY;
  }

  public void setAllownull(String aValue)
  {
    allowNull = new Boolean(aValue).booleanValue();
  }

  public void setSelected(int aValue)
  {
    selected = aValue;
  }

  public void setShowampm(String aValue)
  {
    showampm = new Boolean(aValue).booleanValue();
  }
}
